package org.jash

/**
 * Created by Jash
 */

open class User(var username:String = "u", var password:String = "p", gender:String) {
    var age: Int = 0
        set(value) {
            if (value >= 0 && value <= 150) {
                field = value
            }
        }
        get() = 100
    val gender:String
        get() {
            return field
        }
//    var username:String
    init {
        println("初始化 User")
        this.gender = gender
//        this.username = "用户名:" + username
    }


//    Unit = java中的 void
    @JvmOverloads
    fun login(un:String = "", pw:String):Boolean {
//        == 就是 java中的 equals方法
//        === 才是 java中的==
        return username == un && password == pw
    }

    // 运算符重载
    override fun equals(other: Any?): Boolean {
        println("User.equals")
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as User

        if (username != other.username) return false
        if (password != other.password) return false

        return true
    }

    override fun hashCode(): Int {
        var result = username?.hashCode() ?: 0
        result = 31 * result + password.hashCode()
        return result
    }


}